<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Getting Started with Videos</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d5/d10/tutorial_js_root.html">OpenCV.js Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d04/tutorial_js_table_of_contents_gui.html">GUI Features</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Getting Started with Videos </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Goal </h2>
<ul>
<li>Learn to capture video from a camera and display it.</li>
</ul>
<h2>Capture video from camera </h2>
<p>Often, we have to capture live stream with a camera. In OpenCV.js, we use <a href="https://webrtc.org/">WebRTC</a> and HTML canvas element to implement this. Let's capture a video from the camera(built-in or a usb), convert it into grayscale video and display it.</p>
<p>To capture a video, you need to add some HTML elements to the web page:</p><ul>
<li>a &lt;video&gt; to display video from camera directly</li>
<li>a &lt;canvas&gt; to transfer video to canvas ImageData frame-by-frame</li>
<li>another &lt;canvas&gt; to display the video OpenCV.js gets</li>
</ul>
<p>First, we use WebRTC navigator.mediaDevices.getUserMedia to get the media stream. </p><div class="fragment"><div class="line">let video = document.getElementById(&quot;videoInput&quot;); // video is the id of video tag</div><div class="line">navigator.mediaDevices.getUserMedia({ video: true, audio: false })</div><div class="line">    .then(function(stream) {</div><div class="line">        video.srcObject = stream;</div><div class="line">        video.play();</div><div class="line">    })</div><div class="line">    .catch(function(err) {</div><div class="line">        console.log(&quot;An error occurred! &quot; + err);</div><div class="line">    });</div></div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>This function is unnecessary when you capture video from a video file. But notice that HTML video element only supports video formats of Ogg(Theora), WebM(VP8/VP9) or MP4(H.264).</dd></dl>
<h2>Playing video </h2>
<p>Now, the browser gets the camera stream. Then, we use CanvasRenderingContext2D.drawImage() method of the Canvas 2D API to draw video onto the canvas. Finally, we can use the method in <a class="el" href="../../df/d24/tutorial_js_image_display.html">Getting Started with Images</a> to read and display image in canvas. For playing video, <a class="el" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563" title="Displays an image in the specified window. ">cv.imshow()</a> should be executed every delay milliseconds. We recommend setTimeout() method. And if the video is 30fps, the delay milliseconds should be (1000/30 - processing_time). </p><div class="fragment"><div class="line">let canvasFrame = document.getElementById(&quot;canvasFrame&quot;); // canvasFrame is the id of &lt;canvas&gt;</div><div class="line">let context = canvasFrame.getContext(&quot;2d&quot;);</div><div class="line">let src = new cv.Mat(height, width, cv.CV_8UC4);</div><div class="line">let dst = new cv.Mat(height, width, cv.CV_8UC1);</div><div class="line"></div><div class="line">const FPS = 30;</div><div class="line">function processVideo() {</div><div class="line">    let begin = Date.now();</div><div class="line">    context.drawImage(video, 0, 0, width, height);</div><div class="line">    src.data.set(context.getImageData(0, 0, width, height).data);</div><div class="line">    cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);</div><div class="line">    cv.imshow(&quot;canvasOutput&quot;, dst); // canvasOutput is the id of another &lt;canvas&gt;;</div><div class="line">    // schedule next one.</div><div class="line">    let delay = 1000/FPS - (Date.now() - begin);</div><div class="line">    setTimeout(processVideo, delay);</div><div class="line">}</div><div class="line"></div><div class="line">// schedule first one.</div><div class="line">setTimeout(processVideo, 0);</div></div><!-- fragment --><p>OpenCV.js implements <b><a class="el" href="../../d8/dfe/classcv_1_1VideoCapture.html" title="Class for video capturing from video files, image sequences or cameras. ">cv.VideoCapture</a> (videoSource)</b> using the above method. You need not to add the hidden canvas element manually. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">videoSource</td><td>the video id or element. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><a class="el" href="../../d8/dfe/classcv_1_1VideoCapture.html" title="Class for video capturing from video files, image sequences or cameras. ">cv.VideoCapture</a> instance</dd></dl>
<p>We use <b>read (image)</b> to get one frame of the video. For performance reasons, the image should be constructed with cv.CV_8UC4 type and same size as the video. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">image</td><td>image with cv.CV_8UC4 type and same size as the video.</td></tr>
  </table>
  </dd>
</dl>
<p>The above code of playing video could be simplified as below. </p><div class="fragment"><div class="line">let src = new cv.Mat(height, width, cv.CV_8UC4);</div><div class="line">let dst = new cv.Mat(height, width, cv.CV_8UC1);</div><div class="line">let cap = new cv.VideoCapture(videoSource);</div><div class="line"></div><div class="line">const FPS = 30;</div><div class="line">function processVideo() {</div><div class="line">    let begin = Date.now();</div><div class="line">    cap.read(src);</div><div class="line">    cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);</div><div class="line">    cv.imshow(&quot;canvasOutput&quot;, dst);</div><div class="line">    // schedule next one.</div><div class="line">    let delay = 1000/FPS - (Date.now() - begin);</div><div class="line">    setTimeout(processVideo, delay);</div><div class="line">}</div><div class="line"></div><div class="line">// schedule first one.</div><div class="line">setTimeout(processVideo, 0);</div></div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>Remember to delete src and dst after when stop.</dd></dl>
<h2>Try it </h2>
<p> 
<iframe src="../../js_video_display.html" width="100%"
        onload="this.style.height=this.contentDocument.body.scrollHeight +'px';">
</iframe>
 </p>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
